﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;

namespace LotteCinema.Admin
{
    public partial class QuanLyChucVu : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack == false)
                LaydsCV();
        }

        public void LaydsCV()
        {
            DataTable ds_cv = Timkiem_MA("");
           
            for (int i = 0; i < ds_cv.Rows.Count; i++)
            {
                string str = ds_cv.Rows[i]["MaCV"].ToString();
                DDLMaCV.Items.Add(str);
            }
            grChucVu.DataSource = ds_cv;
            grChucVu.DataBind();
        }


        protected void DDLMaCV_SelectedIndexChanged(object sender, EventArgs e)
        {
            //string s = "select * from CHUCVU where macv='" + DDLMaCV.SelectedItem.ToString() + "'";
            DataTable dt = Timkiem_MA(DDLMaCV.SelectedItem.ToString());
            if (dt.Rows.Count != 0)
            {
                txtTenCV.Text = dt.Rows[0][1].ToString();
                txtQuyen.Text = dt.Rows[0][2].ToString();
            }
        }

        protected void imThem_Click(object sender, ImageClickEventArgs e)
        {
            try
            {
                List<SqlParameter> sqlPara = new List<SqlParameter>();
                sqlPara.Add(new SqlParameter("@tencv", txtTenCV.Text.ToString()));
                sqlPara.Add(new SqlParameter("@quyen", txtQuyen.Text.ToString()));
                CDatastore.ThucHienStore("SP_INSERT_CHUCVU", sqlPara);
                lblthongbao.Text = "Bạn đã thêm Chức Vụ thành công";
                DDLMaCV.Items.Clear();
                txtTenCV.Text = null;
                txtQuyen.Text = null;
                LaydsCV();
            }
            catch (Exception ex)
            {
                lblthongbao.Text = ex.Message;
            }
            
        }

        protected void imXoa_Click(object sender, ImageClickEventArgs e)
        {
            try
            {
                List<SqlParameter> sqlPara = new List<SqlParameter>();
                sqlPara.Add(new SqlParameter("@macv", DDLMaCV.Text.ToString()));
                CDatastore.ThucHienStore("SP_DELETE_CHUCVU", sqlPara);
                lblthongbao.Text = "Bạn đã xóa Mã Chức Vụ " + DDLMaCV.Text + " thành công";
                DDLMaCV.Items.Clear();
                LaydsCV();
                txtTenCV.Text = null;
                txtQuyen.Text = null;

            }
            catch (Exception ex)
            {
                lblthongbao.Text = ex.Message;
            }
        }

        protected void imSua_Click(object sender, ImageClickEventArgs e)
        {
            try
            {
                List<SqlParameter> sqlPara = new List<SqlParameter>();
                sqlPara.Add(new SqlParameter("@macv", DDLMaCV.Text.ToString()));
                sqlPara.Add(new SqlParameter("@tencv", txtTenCV.Text.ToString()));
                sqlPara.Add(new SqlParameter("@quyen", txtQuyen.Text.ToString()));
                CDatastore.ThucHienStore("SP_UPDATE_CHUCVU", sqlPara);
                lblthongbao.Text = "Bạn đã cập nhật Mã Chức Vụ " + DDLMaCV.Text + " thành công";
                DDLMaCV.Items.Clear();
                LaydsCV();
                txtTenCV.Text = null;
                txtQuyen.Text = null;
            }
            catch (Exception ex)
            {
                lblthongbao.Text = ex.Message;
            }
        }

        protected void grChucVu_SelectedIndexChanged(object sender, EventArgs e)
        {
            txtQuyen.Text = null;
            txtTenCV.Text = null;
            DDLMaCV.Text = grChucVu.SelectedRow.Cells[1].Text;
            string tencv = grChucVu.SelectedRow.Cells[2].Text;
            txtTenCV.Text = tencv;
            txtQuyen.Text = grChucVu.SelectedRow.Cells[3].Text.ToString();
        }

        public DataTable Timkiem_Ten(string ten)
        {
            try
            {
                if (ten != null || ten != "")
                {
                    List<SqlParameter> sqlPara = new List<SqlParameter>();
                    sqlPara.Add(new SqlParameter("@tencv", ten));
                    return CDatastore.DocStore("SP_TIMKIEM_CHUCVU_TENCV", sqlPara);
                }
            }
            catch (Exception e)
            {
                lblthongbao.Text = e.Message;
            }
            return CDatastore.DocStore("SP_TIMKIEM_CHUCVU_TENCV");
        }
        public DataTable Timkiem_MA(string ten)
        {
            try
            {
                if (ten != null || ten != "")
                {
                    List<SqlParameter> sqlPara = new List<SqlParameter>();
                    sqlPara.Add(new SqlParameter("@MAcv", ten));
                    return CDatastore.DocStore("SP_TIMKIEM_CHUCVU_MACV", sqlPara);
                }
            }
            catch (Exception e)
            {
                lblthongbao.Text = e.Message;
            }
            return CDatastore.DocStore("SP_TIMKIEM_CHUCVU_MACV");
        }

        protected void grChucVu_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {

            grChucVu.PageIndex = e.NewPageIndex;
            grChucVu.DataBind();
            LaydsCV();
        }

        protected void btTimKiem_Click(object sender, EventArgs e)
        {
            DataTable dt = Timkiem_Ten(txtTenCV.Text);
            grChucVu.DataSource = dt;
            grChucVu.DataBind();
        }
    }
}